<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 同城号
 */
namespace app\allwin\controller\info;
use app\allwin\controller\Common;
use app\allwin\model\AllwinInfoOrder;
use think\facade\Request;
use think\helper\Time;

class Order extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'订单管理','url'=>url("info.order/index")]]);
    }

    /**
     * 列表
     */
    public function index(int $infoid = 0,int $types = 1){
        $condition = [];
        $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
        $condition[] = ['is_del', '=',0];
        $view['keyword']   = Request::param('keyword/s');
        if($view['keyword'] ){
            $condition[] = ['phone','<=', $view['keyword']];
        }
        $view['starttime'] = Request::param('starttime/s');
        $view['endtime']   = Request::param('endtime/s');
        $view['time']      = Request::param('time/d',0);
        if ($view['time']) {
            switch ($view['time']) {
                case 2:
                    list($start, $end) = Time::yesterday();
                    break;
                case 30:
                    list($start, $end) = Time::month();
                    break;
                case 60:
                    list($start, $end) = Time::lastMonth();
                    break;
                default:
                    list($start, $end) = Time::today();
                    break;
            }
            $condition[] = ['create_time', '>=', $start];
            $condition[] = ['create_time', '<=', $end];
        } else {
            if ($view['starttime']) {
                $condition[] = ['create_time', '>=', strtotime($view['starttime'])];
            }
            if ($view['endtime']) {
                $condition[] = ['create_time', '<=', strtotime($view['endtime'])];
            }
        }
        $view['types']     = $types;
        switch ($types) {
            case 1:
                $condition[] = ['paid_at', '=',1];
                $condition[] = ['status', '=', 0];
                break;
            case 2:
                $condition[] = ['paid_at', '=',1];
                $condition[] = ['status', '=', 1];
                break;
            default:
                $condition[] = ['paid_at', '=',0];
                break;
        }
        $view['lists']   = AllwinInfoOrder::where($condition)->order('id desc')->paginate(20, false, ['query' => ['keyword' => $view['keyword'], 'starttime' => $view['starttime'], 'endtime' => $view['endtime'], 'time' => $view['time']]]);
        $view['pay']     = AllwinInfoOrder::where($this->mini_program)->where(['paid_at' => 1])->sum('amount');
        $view['invalid'] = AllwinInfoOrder::where($this->mini_program)->where(['status' => 1])->sum('amount');
        $view['infoid']  = $infoid;
        if($infoid){
            $view['tabs'] = [
                ['name' =>'信息内容','url' =>url('info.index/reply',['id' => $infoid])],
                ['name' =>'订单列表','url' =>url('info.order/index',['infoid' => $infoid]),'action' => 1],
            ];
        }
        return view()->assign($view);
    }

    /**
     * 订单详情
     */
    public function detail(int $id){
        $info         = AllwinInfoOrder::where(['id' => $id])->find();
        $info->field  = json_decode($info->fields, true);
        $view['info'] = $info;
        return view()->assign($view);
    }

    /**
     * 删除
     */
    public function delete($id){
        $result = AllwinInfoOrder::update(['is_del'=>1],['id' => $id]);
        if($result){
            return enjson(200,'操作成功',['url' => url('index',['status' => $this->request->param('status/d')])]);
        }
        return enjson(0,'操作失败');
    }

    /**
     * 订单完成
     */
    public function completion($id){
        $result = AllwinInfoOrder::update(['status'=>1],['id' => $id]);
        if($result){
            return enjson(200,'操作成功',['url' => url('index',['status' => $this->request->param('status/d')])]);
        }else{
            return enjson(0,'操作失败');
        }
    }
}